﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.product
{
    public partial class productbrand_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                else
                {
                    BindProductType("0");
                }
                InitWebControl();

                if (ViewState["ID"] != null)
                {
                    YXShop.Common.PromptInfo.Popedom("001002004", "对不起，您没有权限进行编辑");
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("001002002", "对不起，您没有权限进行新增");
                }
                
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Save();
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入商品名称的名称,您可以对商品进行品牌归类", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "输入数字将作为显示的排列顺序", "isint", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        protected void BandInfo(int id)
        {
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            YXShop.Model.Product.ProductBrand model = bll.GetModelID(id);
            this.txtName.Text = model.Name;
            this.txtDescription.Text = model.Description;
            this.txtSort.Text = model.Sort.ToString();
            this.rdolstIsRecommend.SelectedValue = model.Isrecommend.ToString();
            if (model.Images.Contains("http://"))
            {
                this.rblBrandImagesType.SelectedValue = "1";
                this.txtHttp.Text = model.Images;
                this.trHttp.Style.Value = "display:";
                this.trAddress.Style.Value = "display:none";
            }
            else
            {
                this.rblBrandImagesType.SelectedValue = "0";
                this.trHttp.Style.Value = "display:none";
                this.trAddress.Style.Value = "display:";
            }
            ViewState["ID"] = model.ID;
            ViewState["Images"] = model.Images;
            BindProductType(model.ID.ToString());
            this.cblProductType.Items[0].Selected = model.CommonTypes == 1 ? true : false;
        }
        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
            }
            return reStr;
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        protected void Save()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            YXShop.Model.Product.ProductBrand model = new YXShop.Model.Product.ProductBrand();
            if (this.cblProductType.Items[0].Selected)
            {
                model.CommonTypes = 0;
            }
            else
            {
                model.CommonTypes = 1;
            }
            model.Name = txtName.Text.Trim();
            model.Description = txtDescription.Text.Trim();
            model.Attirbute ="ff";
            model.Isrecommend = ChangeHope.Common.StringHelper.StringToInt(rdolstIsRecommend.SelectedValue);
            model.Sort = ChangeHope.Common.StringHelper.StringToInt(txtSort.Text.Trim());
            if (this.rblBrandImagesType.SelectedValue == "1")
            {
                model.Images = this.txtHttp.Text.Trim();
            }
            else
            {
                ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                uf.ExtensionLim = ".gif,.jpg,.jpeg,.dmp";
                uf.FileLengthLim = sp.ImageSize;
                uf.PostedFile = this.fuBrandImages;
                uf.SavePath = "/yxuploadfile/brand";
                if (uf.Upload())
                {
                    if (uf.HaveLoad)
                    {
                        model.Images = uf.FilePath;
                    }
                    else
                    {
                        if (ViewState["Images"] != null)
                        {
                            model.Images = ViewState["Images"].ToString();
                        }
                        else
                        {
                            model.Images = string.Empty;
                        }
                    }
                }
                else
                {
                    this.ltlMsg.Text = "操作失败，" + uf.Message + "";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (ViewState["ID"] != null)
            {
                model.ID = int.Parse(ViewState["ID"].ToString());
                bll.Update(model);
                UpdateProductType(ViewState["ID"].ToString(), false);
                ChangeHope.WebPage.BasePage.PageRight("信息已修改。", "productbrand_list.aspx");
            }
            else
            {
                int bId = bll.Add(model);
                if (bId > 0)
                {
                    UpdateProductType(bId.ToString(), true);
                    ChangeHope.WebPage.BasePage.PageRight("信息已保存，您还可以继续添加。", "productbrand_edit.aspx");
                }
            }
        }
        /// <summary>
        /// 修改商品类型中关联品牌
        /// </summary>
        /// <param name="brandId"></param>
        /// <param name="isAdd"></param>
        protected void UpdateProductType(string brandId,bool isAdd)
        {
            string strProductTypeId = "";
            for (int j = 0; j < this.cblProductType.Items.Count; j++)
            {
                if (cblProductType.Items[j].Selected)
                {
                    if (strProductTypeId == "")
                    {
                        strProductTypeId = cblProductType.Items[j].Value;
                    }
                    else
                    {
                        strProductTypeId += "," + cblProductType.Items[j].Value;
                    }
                }
            }
            strProductTypeId = "," + strProductTypeId + ",";
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            List<YXShop.Model.Product.ProductType> list = bll.GetAllByWhere("");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    string relevanceBrand = list[i].RelevanceBrand;
                    if (isAdd)
                    {
                        if (strProductTypeId.Contains("," + list[i].Id + ","))
                        {
                            if (list[i].RelevanceBrand != "" && list[i].RelevanceBrand != string.Empty)
                            {
                                relevanceBrand += "," + brandId;
                            }
                            else
                            {
                                relevanceBrand = brandId;
                            }
                            bll.Amend(list[i].Id, "RelevanceBrand", relevanceBrand);
                        }
                    }
                    else
                    {
                        if (!("," + relevanceBrand + ",").Contains("," + brandId + ",") && strProductTypeId.Contains("," + list[i].Id + ","))
                        {
                            if (list[i].RelevanceBrand != "" && list[i].RelevanceBrand != string.Empty)
                            {
                                relevanceBrand += "," + brandId;
                            }
                            else
                            {
                                relevanceBrand = brandId;
                            }
                            bll.Amend(list[i].Id, "RelevanceBrand", relevanceBrand);
                        }
                        else if (("," + relevanceBrand + ",").Contains("," + brandId + ",")&&!strProductTypeId.Contains("," + list[i].Id + ","))
                        {

                                string[] strRelevanceBrand = relevanceBrand.Split(',');
                                string reRelevanceBrand = string.Empty;
                                for (int s = 0; s < strRelevanceBrand.Length; s++)
                                {
                                    if (strRelevanceBrand[s] != brandId && strRelevanceBrand[s] != "")
                                    {
                                        if (reRelevanceBrand != string.Empty)
                                        {
                                            reRelevanceBrand += "," + strRelevanceBrand[s];
                                        }
                                        else
                                        {
                                            reRelevanceBrand = strRelevanceBrand[s];
                                        }
                                    }
                                }
                                bll.Amend(list[i].Id, "RelevanceBrand", reRelevanceBrand);
                        }
                    }

                }
            }
        }

        /// <summary>
        /// 绑定商品类型
        /// </summary>
        /// <param name="bId"></param>
        protected void BindProductType(string bId)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            List<YXShop.Model.Product.ProductType> list=bll.GetAllByWhere("");
            this.cblProductType.Items.Add(new ListItem("通用商品类型", "0"));
            string brandStr = string.Empty;
            if (list.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    j++;
                    this.cblProductType.Items.Add(new ListItem(list[i].TypeName, list[i].Id.ToString()));
                    brandStr =","+list[i].RelevanceBrand+",";
                    if (brandStr != string.Empty)
                    {
                        if (brandStr.Contains("," + bId + ","))
                        {
                            this.cblProductType.Items[j].Selected = true;
                        }
                        
                    }
                }
            }
            if (bId == "0")
            {
                this.cblProductType.SelectedValue = "0";
            }
        }
    }
}
